package com.orangelife.service.propFeePaybillItem.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeePaybillItem.IPropFeePaybillItemService;
import com.orangelife.service.propFeePaybillItem.model.PropFeePaybillItemDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeePaybillItem类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-08-24 17:05:28
 * ==============================================================
 */
@Service("propFeePaybillItemService")
public class PropFeePaybillItemServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeePaybillItemService {
	private static Logger logger = Logger
			.getLogger(PropFeePaybillItemServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeePaybillItemServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeePaybillItem(PropFeePaybillItemDto propFeePaybillItem)
			throws AppBizException {

		Assert.notNull(propFeePaybillItem);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeePaybillItem.getItemName())) {
			if (propFeePaybillItem.getItemName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "细项名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePaybillItem.getRemark())) {
			if (propFeePaybillItem.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		propFeePaybillItem.setItemID(IdUtil.getuuId());
		propFeePaybillItem.setItemStat(TypeConst.FLAG_ON);
		propFeePaybillItem.setTscreate(new Date());
		propFeePaybillItem.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeePaybillItemMapper.insertSelective", propFeePaybillItem);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeePaybillItem(PropFeePaybillItemDto propFeePaybillItem)
			throws AppBizException {

		Assert.notNull(propFeePaybillItem);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeePaybillItem.getItemID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeePaybillItemDto oldPropFeePaybillItem = this
				.findPropFeePaybillItemByID(propFeePaybillItem.getItemID());

		if (!StringUtils.isBlank(propFeePaybillItem.getItemName())) {
			if (propFeePaybillItem.getItemName().length() > 128) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "细项名称长度超出");
			}
		}

		if (!StringUtils.isBlank(propFeePaybillItem.getRemark())) {
			if (propFeePaybillItem.getRemark().length() > 256) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "备注长度超出");
			}
		}

		if (oldPropFeePaybillItem == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeePaybillItem.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePaybillItemMapper.updateByPrimaryKeySelective",
				propFeePaybillItem);
	}

	@Override
	public PropFeePaybillItemDto findPropFeePaybillItemByID(
			String propFeePaybillItemID) throws AppBizException {
		if (StringUtils.isBlank(propFeePaybillItemID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePaybillItemDto oldPropFeePaybillItem = this.getSqlSession()
				.selectOne("PropFeePaybillItemSearchMapper.selectByPrimaryKey",
						propFeePaybillItemID);
		if (oldPropFeePaybillItem == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeePaybillItem.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeePaybillItem;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeePaybillItemByLogic(String propFeePaybillItemID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeePaybillItemID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeePaybillItemDto oldPropFeePaybillItem = this
				.findPropFeePaybillItemByID(propFeePaybillItemID);

		oldPropFeePaybillItem.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeePaybillItem.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeePaybillItemMapper.updateByPrimaryKeySelective",
				oldPropFeePaybillItem);
	}

	@Override
	public List<Map<String, Object>> findPropFeePaybillItemByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePaybillItemSearchMapper.findAllPropFeePaybillItem",
						map);
		return list;
	}

	@Override
	public List<PropFeePaybillItemDto> getPropFeePaybillItemByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeePaybillItemDto> list = this.getSqlSession().selectList(
				"PropFeePaybillItemSearchMapper.getAllPropFeePaybillItemDto",
				map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeePaybillItemByRelevantID(
			String itemID, String paybillID, String itemType, String type,
			String linkID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("itemID", itemID);
		map.put("paybillID", paybillID);
		map.put("itemType", itemType);
		map.put("type", type);
		map.put("linkID", linkID);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePaybillItemSearchMapper.selectPropFeePaybillItemDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeePaybillItemDto> findPropFeePaybillItemByRelevantID(
			String itemID, String paybillID, String itemType, String type,
			String linkID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("itemID", itemID);
		map.put("paybillID", paybillID);
		map.put("itemType", itemType);
		map.put("type", type);
		map.put("linkID", linkID);

		List<PropFeePaybillItemDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeePaybillItemSearchMapper.findPropFeePaybillItemDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeePaybillItemDto> bauthfindPropFeePaybillItem(
			List<String> propFeePaybillItemIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeePaybillItemDto> list = this.getSqlSession().selectList(
				"PropFeePaybillItemSearchMapper.batchFindPropFeePaybillItem",
				propFeePaybillItemIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeePaybillItemByMap(
			Map<String, Object> propFeePaybillItem) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeePaybillItemSearchMapper.batchFindPropFeePaybillItemByMap",
						propFeePaybillItem);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	public int addPropFeePaybillItems(
			List<PropFeePaybillItemDto> propFeePaybillItems)
			throws AppBizException {
		
		int n=this.getSqlSession().insert("PropFeePaybillItemBauthMapper.bauthInsert", propFeePaybillItems);

		// TODO Auto-generated method stub
		return n;
	}

}